package com.xc.domain.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.xc.domain.pojo.User;
import com.xc.domain.query.PageQuery;
import com.xc.domain.service.IUserService;
import com.xc.simpleReflect.Input.UserInput;
import com.xc.simpleReflect.utils.SimpleReflect;
import com.xc.vo.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.Get;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xc
 * @since 2024-05-31
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Tag(name = "用户接口")
@Slf4j
public class UserController {

    private final IUserService userService;

    @PostMapping("/page")
    @Operation(summary = "分页查询")
    public ResultVO<List<User>> page(@RequestBody PageQuery pageQuery) {
        IPage<User> page = userService.lambdaQuery().page(Page.of(pageQuery.getPageNo(), pageQuery.getPageSize()));
        log.info("page的Size：{}", page.getRecords().size());
        return ResultVO.success(page.getRecords());
    }

    @PostMapping("pa")
    @Operation(summary = "测试@Schema注解的name与title的区别")
    public void pa(@RequestBody User user) {

    }

    @Operation(summary = "查询详情")
    @GetMapping("/getDetail")
    public ResultVO<User> getDetail(Long id) {
        User user = Db.lambdaQuery(User.class)
                .eq(id != null, User::getId, id)
                .one();
        return ResultVO.success(user);
    }

    @Operation(summary = "测试反射")
    @PostMapping("/getUser")
    public ResultVO<List<User>> getUser(@RequestBody UserInput userInput) {
        QueryWrapper queryWrapper = SimpleReflect.parseWrap(userInput, User.class);
        List<User> list = userService.list(queryWrapper);
        return ResultVO.success(list);
    }

    @Operation(summary = "分页模糊查询")
    @GetMapping("/getPage")
    public ResultVO<List<User>> getPage(UserInput userInput, PageQuery pageQuery) {
        QueryWrapper queryWrapper = SimpleReflect.parseWrap(userInput, User.class);
        Page<User> userPage = new Page<>(Long.valueOf(pageQuery.getPageNo()), Long.valueOf(pageQuery.getPageSize()));
        IPage page = userService.page(userPage, queryWrapper);
        log.info("page的Size：{}", page.getRecords().size());
        return ResultVO.success(page.getRecords());
    }
}
